package com.wowangz.cms.syscore.autotask.job;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;

import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.autotask.service.StoreDatabaseResourceService;
import com.wowangz.cms.syscore.pojo.RouteInfo;
import com.wowangz.cms.syscore.utils.DataPageUtil;
import com.wowangz.cms.syscore.utils.JedisUtil;

public class StoreDatabaseResourceSync extends BaseJob {

	/**
	 * 同步商户数据源信息
	 */
	@Override
	public void innerExecute(JobExecutionContext context) throws Exception {
		int pageStartRow=0,pageEndRow=0;
		Map query = new HashMap();
		query.put("state",SysConstant.CommonApproveStatus.OK);
		query.put("statusid",SysConstant.RecordStatus.VALID);
		StoreDatabaseResourceService storeDatabaseResourceService = (StoreDatabaseResourceService) IssTransactionContainer.getInstance(StoreDatabaseResourceService.class, false);
		long count=storeDatabaseResourceService.queryStoreDatabaseResourceCount(query);
		int page=DataPageUtil.totalPages(Long.valueOf(count).intValue());
		for (int i = 1; i < page+1; i++) {
			pageStartRow=DataPageUtil.pageNum *(i-1);
			pageEndRow=DataPageUtil.pageNum ;
			query.put("startrow",pageStartRow);
			query.put("endrow",pageEndRow);
			List<RouteInfo> list = storeDatabaseResourceService.queryStoreDatabaseResource(query);
			for (RouteInfo routeInfo : list){
				this.getUrl(routeInfo);
				JedisUtil.insertObject(routeInfo.getStoreid().toString(), routeInfo, JedisUtil.CONT_CLUSTERNAME_USERROUTE);
			}
		}
	}
	
	private String getUrl(RouteInfo routeInfo)
	{
		StringBuffer url = new StringBuffer("jdbc:mysql://");
		url.append(routeInfo.getIp());
		url.append(":");
		url.append(routeInfo.getPort());
		url.append("/");
		url.append(routeInfo.getDbname());
		url.append("?useUnicode=true&characterEncoding=utf-8");
		System.out.println("redis中的数据源" + url);
		return url.toString();
	}

}
